<?php
class XA_XHTML_Menu extends XA_XHTML {

	protected $item_list = array();
	public $active;



	public function __construct() {
		$this->addClass('menu');
	}



	public function out() {
		if ($this->active) {
			if (array_key_exists($this->active, $this->item_list)) {
				$this->item_list[$this->active]['active'] = true;
			} else {
				e('Pozycja '.$this->active.' w menu nie istnieje.', $this->item_list);
			}
		} else {
			$this->item_list[current(array_keys($this->item_list))]['active'] = true;
		}

		foreach ($this->item_list as $item) {
			$list_items[] = "\n"
				. '<li class="'
				. (!empty($item['active']) ? 'active ' : '')
				. implode(' ', $item['class_list'])
				. '"><a href="'.$item['url'].'">'
				. $item['text']
				. '</a></li>';
		}

		return '<ul class="'.implode(' ', $this->class_list).'">'.implode('', $list_items).'</ul>';
	}



	public function addItem($index, $url, $text = null, $class_list = array()) {
		if (is_string($class_list)) {
			$class_list = explode(' ', $class_list);
		}

		$class_list += array($index);

		$this->item_list[$index] = array(
			  'url' => $url
			, 'text' => $text ?: $url
			, 'class_list' => $class_list
		);
	}
}
